package com.screen.screendisplay.service.impl.dryport;

import com.screen.screendisplay.entity.dryport.DryportProdoperPortGather;
import com.screen.screendisplay.mapper.dryport.DryportProdoperPortGatherMapper;
import com.screen.screendisplay.service.dryport.DryportProdoperPortGatherService;
import com.screen.screendisplay.vo.dryport.PortGatherVO;
import com.screen.screendisplay.vo.wholeproduct.PortGatheringVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 集港量情况表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class DryportProdoperPortGatherServiceImpl extends ServiceImpl<DryportProdoperPortGatherMapper, DryportProdoperPortGather> implements DryportProdoperPortGatherService {

    @Override
    public List<PortGatherVO> getMonthlyStatsByYear(Integer year) {
        // 从数据库查询数据
        List<DryportProdoperPortGather> entityList = baseMapper.getMonthlyStatsByYear(year);
        
        // 转换为VO对象
        return entityList.stream().map(entity -> {
            PortGatherVO vo = new PortGatherVO();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<PortGatheringVO> getAnnualPortGathering() {
        // 从数据库查询年度汇总数据
        List<DryportProdoperPortGather> annualStatsList = baseMapper.getAnnualStats();
        
        // 构建结果列表
        List<PortGatheringVO> resultList = new ArrayList<>();
        
        // 遍历查询结果，计算同比率
        for (int i = 0; i < annualStatsList.size(); i++) {
            DryportProdoperPortGather current = annualStatsList.get(i);
            PortGatheringVO vo = new PortGatheringVO();
            
            vo.setYear(current.getYear());
            vo.setGatheringPlanned(current.getGatheringPlanned());
            vo.setGatheringCompleted(current.getGatheringCompleted());
            
            // 计算同比率：(当年完成量 - 去年完成量) / 去年完成量 * 100
            // 注意：i+1 是因为数据是按年份降序排列的
            if (i + 1 < annualStatsList.size()) {
                DryportProdoperPortGather previous = annualStatsList.get(i + 1);
                BigDecimal currentCompleted = current.getGatheringCompleted();
                BigDecimal previousCompleted = previous.getGatheringCompleted();
                
                if (previousCompleted != null && previousCompleted.compareTo(BigDecimal.ZERO) != 0) {
                    BigDecimal yoyRate = currentCompleted.subtract(previousCompleted)
                            .divide(previousCompleted, 4, RoundingMode.HALF_UP)
                            .multiply(new BigDecimal("100"))
                            .setScale(2, RoundingMode.HALF_UP);
                    vo.setYoyRate(yoyRate);
                } else {
                    vo.setYoyRate(BigDecimal.ZERO);
                }
            } else {
                // 第一年没有同比数据
                vo.setYoyRate(BigDecimal.ZERO);
            }
            
            resultList.add(vo);
        }
        
        return resultList;
    }

}
